package com.common.core.manage;

import android.app.Activity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by hai8108 on 2015/6/1.
 */
public class CommonActivityManager {
    protected static Map<String,Activity> map = new HashMap<>();

    public static void addActicity(String tag,Activity activity){
        if(map!=null){
            map.put(tag, activity);
        }
    }
    public static void removeActicity(String tag){
        if(map!=null){
            map.remove(tag);
        }
    }
    public static void destoryActivity(String tag){
        if(map!=null){
            Activity activity = map.get(tag);
            if(activity != null){
                activity.finish();
                map.remove(tag);
            }
        }
    }
    public static void destoryActivityExceptTag(String tag){
        if(map!=null && map.size() > 0){
            Map<String,Activity> mapTmp = new HashMap<>();
            mapTmp.putAll(map);
            Iterator<Map.Entry<String, Activity>> it = mapTmp.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Activity> entry = it.next();
                Activity activity = entry.getValue();
                String taghas = entry.getKey();
                if(!taghas.equals(tag)){
                    activity.finish();
                    map.remove(taghas);
                }
            }
        }
    }
    public static void destory(){
        if(map!=null && map.size() > 0){
            Iterator<Map.Entry<String, Activity>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Activity> entry = it.next();
                Activity activity = entry.getValue();
                activity.finish();
            }
            map.clear();
        }
    }
}
